<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of tourtype
 *
 * @author Pham Minh Khoi
 * @email: pmkhoi@gmail.com
 * @package: 
 */
 if (!defined('BASEPATH'))
	exit ('No direct script access allowed');

class TourType extends MY_Controller {
    function __construct() {

        parent::__construct();

        $this->checkLogin();
        $this->checkPermission();

        $this->load->model('tour_type_model', 'type', true);
        $this->load->helper('form');
        $this->template->set_template('admin');
        $this->buildMenu();
    }


	function index() {
		$this->view();
	}

	function create(){
		$data = array();
		$data['id'] = 0;
                $data['code'] = '';
		$data['vi_name'] = '';
		$data['en_name'] = '';
                $data['checked'] = 'check';
                $data['position'] = $this->type->getMaxPosition() + 1;
                $data['error_message'] = '';
		$data['legend_text'] = lang('create_source');
		$this->template->parse_view('content', 'admin/tour_type/create_tour_type', $data);
                $this->template->render();
	}

	function edit($id_country) {
		$data = $this->type->getById($id_country);
		if(!$data){
			redirect('admin/tourtype');
			return;
		}

		$data['checked'] = $data['active'] == 1 ? 'checked' : '';
		$data['error_message'] = '';
		$data['legend_text'] = lang('edit_source');

                $this->template->parse_view('content', 'admin/tour_type/create_tour_type', $data);
                $this->template->render();
		//$this->main =  $this->load->view('admin/source/create_source', $data, true);
		//$this->execute();
	}

	function save() {
		$id = (int) $this->input->post('txtID');
		$source = new stdClass();
                $data = array();
		if($id > 0){
			$type = $this->type->getById($id);
			if(!$type){
				redirect('admin/tourtype');
				return;
			}
                        $data = $type;
		}
		else{
			$data['id'] = 0;
		}

                $data['id'] = (int) $this->input->post('txtID');
                $data['code'] = trim($this->input->post('txtCode'));
		$data['vi_name'] = trim($this->input->post('txtViName'));
		$data['en_name'] = trim($this->input->post('txtEnName'));
		$data['checked'] = trim($this->input->post('ckActive')) == 'on' ? 'checked' : '';
                $data['active'] = trim($this->input->post('ckActive')) == 'on' ? 1 : 0;
                $data['en_name'] = trim($this->input->post('txtEnName'));
                $data['position'] = intval($this->input->post('txtPosition'));
		$error = false;
		$error_message = '';

		$this->load->library('form_validation');
                $this->form_validation->set_rules('txtCode', 'lang:iso2', 'required|min_length[2]|max_length[50]');
		$this->form_validation->set_rules('txtViName', 'lang:vi_name', 'required|min_length[3]|max_length[50]');
		$this->form_validation->set_rules('txtEnName', 'lang:en_name', 'required|min_length[3]|max_length[50]');

		if ($this->form_validation->run() == FALSE ){
			$data['error_message'] = '<div style="color:red">' . validation_errors().'</div>';
			$data['legend_text'] = $id > 0 ?  lang('edit_source') : lang('create_source');

                        $this->template->parse_view('content', 'admin/tour_type/create_tour_type', $data);
                        $this->template->render();
		}
		else{
			if($id > 0){
				$result = $this->type->update($data);
                               
			}
			else{
				$id_source = $this->type->insert($data);
			}

			redirect('admin/tourtype/');
		}

	}

	function view() {
		$NUMBER_RECORD_PER_PAGE = 15;
		$this->load->library('pagination');
		$page = (int)$this->uri->segment(4);
		$config['base_url'] = $this->base_url . 'admin/tourtype/view';
		$config['total_rows'] = $this->type->getAllCount();
		$config['per_page'] = $NUMBER_RECORD_PER_PAGE;
		$config['uri_segment'] = 4;
		$config['first_link'] = 'First';

		$this->pagination->initialize($config);

		$rows = $this->type->getAll($page, $NUMBER_RECORD_PER_PAGE);
		$data = array();
		$data['rows'] = $rows;

                $this->template->parse_view('content', 'admin/tour_type/tour_list', $data);
                $this->template->render();
                //$this->main = $this->load->view('admin/source/sources_list', $data, true);
		//$this->execute();
	}

	function delete(){
		$id = intval($this->uri->segment(4));
		$ids = array();
 		if($id>0){
			$ids[] = (int)$id;
 			//$this->product->delete($id);
		}
 		else{
 			if(($this->input->post('submitDelcategory'))){
 				$ids = $this->input->post('categoryBox');
 			}
 		}

		if(count($ids)){

 			$result = $this->country->delete($ids);

		}

 		redirect('admin/country/');
	}
}
?>
